const tKey = 'token-key';
const bUser = 'book_user'

export default{
  state:{
    // 用户token
    token: localStorage.getItem(tKey) || "",
    // 用户信息
    userInfo: JSON.parse(localStorage.getItem(bUser)) || {}
  },
  getters:{

  },
  mutations:{
    // 设置token
    setToken(state,token){
      state.token = token;
      localStorage.setItem(tKey,token);
    },
    // 设置用户信息
    async setUserInfo(state,info){
      state.userInfo = info;
      await new Promise((resolve) => {
        localStorage.setItem(bUser, JSON.stringify(state.userInfo));
        resolve();
      });
    },
    // 清除
    clearUser(){
      localStorage.removeItem(tKey);
      localStorage.removeItem(bUser);
    },
  },
  actions:{
    async asySetUserInfo({ commit }, data) {
      commit('setUserInfo', data)
    },
    outLogin({ commit }){
      commit('clearUser')
    }

  },
}
